package com.mn.ble.c

import android.annotation.SuppressLint
import android.bluetooth.BluetoothManager
import android.bluetooth.le.ScanCallback
import android.bluetooth.le.ScanResult
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.mn.ble.c.ad.BleAdapter
import com.mn.ble.c.ad.BleBean


class MainActivity : AppCompatActivity() {


    companion object {
        fun jump(c: Context) {
            c.startActivity(Intent(c, MainActivity::class.java))
        }
    }


    private val scanCallback = object : ScanCallback() {
        @SuppressLint("NotifyDataSetChanged", "MissingPermission")
        override fun onScanResult(callbackType: Int, result: ScanResult?) {
            super.onScanResult(callbackType, result)
            if (result!!.device!!.name == null) {
                return
            }
            val b = BleBean(result.device, result)
            if (!mAdapter.contains(b)) {
                mAdapter.addData(b)
            }
        }
    }

    private var mBtn: Button? = null
    private var mRv: RecyclerView? = null
    private val mAdapter = BleAdapter()
    private var isScanning = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        check()

        mBtn = findViewById(R.id.scan)
        mBtn?.setOnClickListener { scan() }

        mRv = findViewById(R.id.rv)
        mRv?.adapter = mAdapter
        mRv?.layoutManager = LinearLayoutManager(this)

    }

    @SuppressLint("MissingPermission")
    private fun scan() {
        if (packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            // 获取蓝牙管理服务
            val bluetoothManager = getSystemService(BLUETOOTH_SERVICE) as BluetoothManager
            // 获取蓝牙适配器
            val bluetoothAdapter = bluetoothManager.adapter
            if (bluetoothAdapter != null) {
                // 判断蓝牙是否开启
                if (bluetoothAdapter.isEnabled) {
                    // 获取扫描器实例
                    val bluetoothLeScanner = bluetoothAdapter.bluetoothLeScanner
                    if (bluetoothLeScanner != null) {
                        if (isScanning) {
                            // 停止扫描
                            isScanning = false
                            bluetoothLeScanner.stopScan(scanCallback)
                            mBtn?.setText("停止")
                        } else {
                            // 开始扫描
                            isScanning = true
                            bluetoothLeScanner.startScan(scanCallback)
                            mBtn?.setText("开始扫描")
                        }
                    }
                } else {
                    Toast.makeText(this, "蓝牙未开启", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    @SuppressLint("MissingPermission")
    private fun check() {
        // 判断支持蓝牙功能
        if (packageManager.hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            // 获取蓝牙管理服务
            val bluetoothManager = getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager
            // 获取蓝牙适配器
            val bluetoothAdapter = bluetoothManager.adapter
            if (bluetoothAdapter != null) {
                // 判断蓝牙是否开启
                if (!bluetoothAdapter.isEnabled) {
                    bluetoothAdapter.enable()
                }
            }
        }
    }


}